<template>
  <div class="comment-reply">
    <!-- 导航栏 -->
    <van-nav-bar :title="`${comment.reply_count}条回复`">
      <van-icon slot="left" name="cross" @click="$emit('click-close')" />
    </van-nav-bar>
    <!-- /导航栏 -->

    <div class="scroll-wrap">
      <!-- 当前评论项 -->
      <CommentItem :comment="comment" />
      <!-- /当前评论项 -->

      <van-cell title="所有回复" />

      <!-- 评论的回复列表 -->
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :error="error"
        error-text="加载失败了，请点击重试"
        @load="onLoad"
      >
        <CommentItem
          v-for="(item, ind) in list"
          :key="ind"
          :comment="item"
        ></CommentItem>
      </van-list>
      <!-- /评论的回复列表 -->
    </div>

    <!-- 底部 -->
    <!-- 发布评论 -->
    <div class="post-wrap">
      <van-button
        size="small"
        round
        class="post-btn"
        @click="isReplyShow = true"
        >写评论</van-button
      >
    </div>
    <!-- /发布评论 -->
    <!-- /底部 -->

    <!-- 发布回复弹出层 -->
    <van-popup v-model="isReplyShow" position="bottom">
      <CommentPost
        :target="comment.com_id"
        @addCommentSuccess="onPostsuccess"
      />
    </van-popup>
    <!-- /发布回复弹出层 -->
  </div>
</template>

<script>
import CommentItem from './common-item.vue';
import { getComments } from '@/api/common';
import CommentPost from './commonPost.vue';
// import CommentReply from "./common-list.vue"
export default {
  name: 'CommentReply',
  components: {
    CommentItem,
    CommentPost
  },
  props: {
    comment: {
      type: Object,
      require: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      offset: null,
      limit: 20,
      isReplyShow: false
    };
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onLoad () {
      this.loading = true;
      try {
        const { data } = await getComments({
          type: 'c',
          source: this.comment.com_id.toString(),
          offset: this.offset,
          limit: this.limit
        });
        const { results } = data.data;
        console.log(results);
        this.list.push(...results);
        this.loading = false;
        if (results.length) {
          this.offset = data.data.last_id;
        } else {
          this.finished = true;
        }
      } catch (error) {
        this.$toast('加载失败请稍后重试');
      }
    },
    onPostsuccess (data) {
      this.comment.reply_count++;
      this.isReplyShow = false;
      this.list.unshift(data.new_obj);
    }
  }
};
</script>

<style scoped lang="less">
.scroll-wrap {
  position: fixed;
  left: 0;
  top: 92px;
  right: 0;
  bottom: 88px;
  overflow-y: auto;
}
.post-wrap {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 88px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-top: 1px solid #d8d8d8;
  .post-btn {
    width: 60%;
  }
}
</style>
